package com.hgy.linkedlist;

public class SingleLinkedListDemo {
    public static void main(String[] args){
        //进行测试
        //先创建节点
        HeroNode hero1 = new HeroNode(1,"宋江", "及时雨");
        HeroNode hero2 = new HeroNode(2,"卢俊义", "玉麒麟");
        HeroNode hero3 = new HeroNode(3,"吴用", "智多星");
        HeroNode hero4 = new HeroNode(4,"林冲", "豹子头");
        HeroNode hero5 = new HeroNode(5,"林冲", "豹子头");
        HeroNode hero6 = new HeroNode(6,"林冲", "豹子头");
        HeroNode hero7 = new HeroNode(7,"林冲", "豹子头");
        HeroNode hero8 = new HeroNode(8,"林冲", "豹子头");

        //创建链表
        SingleLinkedList singleLinkedList1 = new SingleLinkedList();
        SingleLinkedList singleLinkedList2 = new SingleLinkedList();


        //加入节点
//        singleLinkedList.add(hero1);
//        singleLinkedList.add(hero4);
//        singleLinkedList.add(hero2);
//        singleLinkedList.add(hero3);

        //按照编号加入结点
        singleLinkedList1.addByOrder(hero2);
        singleLinkedList1.addByOrder(hero4);
        singleLinkedList1.addByOrder(hero6);
        singleLinkedList1.addByOrder(hero7);
        //输出链表
        singleLinkedList1.list();


        singleLinkedList2.addByOrder(hero1);
        singleLinkedList2.addByOrder(hero3);
        singleLinkedList2.addByOrder(hero5);
        singleLinkedList2.addByOrder(hero8);
        //输出链表
        singleLinkedList2.list();

        //测试合并有序链表
        mergeSortLinkedList(singleLinkedList1.getHead(),singleLinkedList2.getHead());
        System.out.println("合并之后的有序链表");
        singleLinkedList1.list();

//        //测试修改节点的代码
//        HeroNode newHeroNode = new HeroNode(2,"小卢", "玉麒麟");
//        singleLinkedList.update(newHeroNode);
//        System.out.println("修改后的链表情况~~");
//        singleLinkedList.list();

//        //测试删除节点的代码
//        singleLinkedList.del(1);
//        //singleLinkedList.del(2);
//        //singleLinkedList.del(3);
//        //singleLinkedList.del(4);
//        System.out.println("删除后的链表情况~~");
//        singleLinkedList.list();

//        //测试求单链表中有效节点的个数
//        System.out.println(getLength(singleLinkedList.getHead()));

//        //测试求倒数第k个节点
//        HeroNode res = findLastIndexNode(singleLinkedList.getHead(), 1);
//        System.out.println("res" + res);

//        //测试链表反转
//        reverse(singleLinkedList.getHead());
//        System.out.println("反转之后的链表");
//        singleLinkedList.list();
    }

    /**
     * 合并两个有序链表，使合并后依然有序
     * 合并后链表头节点为head1
     * @param head1
     * @param head2
     * head1→2→5→7→8  head2→1→3→4→6→9  temp1=head1  temp2=1
     *
     */
    public static void mergeSortLinkedList(HeroNode head1, HeroNode head2){
        HeroNode temp1 = head1;
        HeroNode temp2 = head2.getNext();
        while(temp1.getNext()!=null && temp2!=null){
            while(temp1.getNext()!=null && temp1.getNext().getNo()<=temp2.getNo()){
                temp1 = temp1.getNext();
            }
            if(temp1.getNext()!=null){
                HeroNode temp3 = temp2.getNext();
                temp2.setNext(temp1.getNext());
                temp1.setNext(temp2);
                temp2 = temp3;
            }else{
                while(temp2!=null) {
                    temp1.setNext(temp2);
                    temp2 = temp2.getNext();
                }
            }
        }
    }

    /**
     * head→A→B→C→D→E
     * newhead→A
     * newhead→B→A
     * newhead→E→D→C→B→A
     */
    public static void reverse(HeroNode head){
        if(head.getNext() == null || head.getNext().getNext() == null){
            return;
        }
        HeroNode temp1 = head.getNext();
        HeroNode temp2 = temp1.getNext();
        temp1.setNext(null);
        while(temp2 != null) {
            HeroNode temp = temp2.getNext();
            temp2.setNext(temp1);
            temp1 = temp2;
            temp2 = temp;
        }
        head.setNext(temp1);
    }

    //查找单链表倒数第k个节点
    //单链表只能从前往后遍历
    //可将倒数第k个节点转化为第size-k+1个节点
    /**
     * @param head 链表的头节点 index 倒数第index个节点
     * @return 返回倒数第index个节点
     */
    public static HeroNode findLastIndexNode(HeroNode head, int index){
        int size = getLength(head);
        if(size<index || index <= 0){
            System.out.println();
            return null;
        }
        HeroNode temp = head.getNext();
        for (int i=0; i<size-index; i++){
            temp = temp.getNext();
        }
        return temp;
    }

    //方法：获取单链表的有效节点个数
    /**
     * @param head 链表的头结点
     * @return 返回有效节点的个数
     */
    public static int getLength(HeroNode head){
        int cpunt = 0;
        HeroNode temp = head;
        while(temp.getNext() != null){
            temp = temp.getNext();
            cpunt++;
        }
        return cpunt;
    }
}

//定义SingleLinkedList 管理我们的英雄
class SingleLinkedList{
    //先初始化一个头结点，头结点不要动
    private HeroNode head = new HeroNode();

    public HeroNode getHead() {
        return head;
    }

    //添加节点到单向链表（不考虑编号顺序）
    //1.找到当前链表的最后节点
    //2.将这个最后节点的next指向新的节点
    public void add(HeroNode heroNode){
        HeroNode temp = head;
        while(temp.getNext()!=null){
            temp = temp.getNext();
        }
        temp.setNext(heroNode);
    }

    //考虑编号顺序添加节点
    public void addByOrder(HeroNode heroNode){
        HeroNode temp = head;
        while(temp.getNext()!=null && temp.getNext().getNo() <= heroNode.getNo()){
            if(temp.getNext().getNo() == heroNode.getNo()){
                System.out.printf("准备插入的英雄的编号%d已经存在，不能加入\n", heroNode.getNo());
                return;
            }
            temp = temp.getNext();
        }
        heroNode.setNext(temp.getNext());
        temp.setNext(heroNode);
    }

    //修改节点的信息，根据no编号来修改，即no编号不能改
    //1.根据newHeroNode的no来修改
    public void update(HeroNode newHeroNode){
        //判断是否为空
        if (head.getNext() == null){
            System.out.println("链表为空~");
            return;
        }
        //找到需要修改的节点，根据no编号
        HeroNode temp = head.getNext();
        while (temp!=null && temp.getNo() != newHeroNode.getNo()){
            temp = temp.getNext();
        }
        if (temp == null){
            System.out.printf("没有找到编号%d的节点，不能修改", newHeroNode.getNo());
        }else {
            temp.setName(newHeroNode.getName());
            temp.setNickname(newHeroNode.getNickname());
        }

    }

    //删除节点
    //跟据no编号进行删除
    public void del(int no){
        HeroNode temp = head;
        while(temp.getNext()!=null && temp.getNext().getNo() != no){
            temp = temp.getNext();
        }
        if (temp.getNext() != null){
            temp.setNext(temp.getNext().getNext());
        }else {
            System.out.printf("要删除的%d节点不存在~\n", no);
        }
    }

    //显示链表，遍历
    public void list(){
        //判断链表是否为空
        if (head.getNext() == null){
            System.out.println("链表为空");
            return;
        }
        HeroNode temp = head.getNext();
        while(temp!=null){
            System.out.println(temp);
            temp = temp.getNext();
        }

    }
}

//定义HeroNode，每个HeroNode对象就是一个节点
class HeroNode{
    private int no;
    private String name;

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public void setNext(HeroNode next) {
        this.next = next;
    }

    public String getName() {
        return name;
    }

    public String getNickname() {
        return nickname;
    }

    public HeroNode getNext() {
        return next;
    }

    private String nickname;
    private HeroNode next;//指向下一个节点

    public HeroNode(){}

    public HeroNode(int no, String name, String nickname){
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +

                '}';
    }
}